package edu.unsw.comp9321.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.unsw.comp9321.common.ServiceLocatorException;
import edu.unsw.comp9321.logic.Controller;

public class RestrictionDAOImpl implements RestrictionDAO {

	private Connection connection;
	
	public RestrictionDAOImpl() throws ServiceLocatorException, SQLException{
		connection = DBConnectionFactory.getConnection();
	}
	
	@Override
	public int findRestrictionAndExcess(int customerTypeId, int carTypeId) {
		int excess = Controller.NO_VIOLATION_CODE;
		try {
			String query = "SELECT additional_excess FROM restriction " +
					"WHERE customer_type = ? AND car_type = ?;";
			PreparedStatement stmnt = connection.prepareStatement(query);
			stmnt.setInt(1, customerTypeId);
			stmnt.setInt(2, carTypeId);
			ResultSet res = stmnt.executeQuery();
			while (res.next()) {
				excess = res.getInt("additional_excess");
			}
			res.close();
			stmnt.close();
		} catch (Exception e) {
			System.out.println("Caught Exception");
			e.printStackTrace();
		}
		return excess;
	}

}
